import {
  MessageBox
} from 'mint-ui';

function getKey(idx) {
  return window.localStorage.key(idx);
}

function setItem(key, val) {
  window.localStorage.setItem(key, val);
}

function getItem(key) {
  return window.localStorage.getItem(key);
}

function removeItem(key) {
  return new Promise((resolve, reject) => {
    MessageBox.confirm('确定删除此留言?')
      .then(action => {
        if (action == "confirm") {
          resolve(action)
          window.localStorage.removeItem(key);
        }
      }).catch(err => {
        reject(err)
      });
  })
}

function removeItem2(key){ 
  window.localStorage.removeItem(key);
}

function clearLocalStorage() {
  return new Promise((resolve, reject) => {
    MessageBox.confirm('确定清空所有留言?')
      .then(action => {
        if (action == "confirm") {
          resolve(action)
          window.localStorage.clear();
        }
      }).catch(err => {
        reject(err)
      });
  })
}

function localStorageLen() {
  return window.localStorage.length;
}

export {
  getKey,
  setItem,
  getItem,
  removeItem,
  removeItem2,
  clearLocalStorage,
  localStorageLen
}